package com.gepengjun.lims.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gepengjun.lims.entity.Orgnazation;
import com.gepengjun.lims.mapper.OrgnazationMapper;
import com.gepengjun.lims.service.OrgnazationService;
import com.gepengjun.lims.service.impl.base.BaseOrgnazationServiceImpl;
import com.gepengjun.lims.service.impl.base.BaseServiceImpl;
import com.gepengjun.lims.service.impl.base.BaseUserServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jun
 * @since 2018-08-19
 */
@Service
public class OrgnazationServiceImpl extends BaseOrgnazationServiceImpl {

    @Resource
    private  OrgnazationMapper orgnazationMapper;

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> findAllAvailableCompany() {
        EntityWrapper<Orgnazation> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("available",Boolean.TRUE);
        entityWrapper.eq("parent_id",0);
        return orgnazationMapper.selectList(entityWrapper);
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> initDeptList(Long orgId) {
        EntityWrapper<Orgnazation> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("available",Boolean.TRUE);
        entityWrapper.eq("parent_id",orgId);
        return orgnazationMapper.selectList(entityWrapper);
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> findAllAvailableDept() {
        EntityWrapper<Orgnazation> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("available",Boolean.TRUE);
        entityWrapper.ne("parent_id",0);
        return orgnazationMapper.selectList(entityWrapper);
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public Orgnazation findDeptByUserId(Long userId) {
        return orgnazationMapper.findDeptByUserId(userId);
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public Page<Orgnazation> findOrgnazationPage(Page<Orgnazation> page, Orgnazation orgnazation) {
        EntityWrapper<Orgnazation> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(orgnazation.getOrgName())){
            wrapper.like("org_name",orgnazation.getOrgName());
        }
        if (StringUtils.isNotEmpty(orgnazation.getOrgCode())){
            wrapper.like("org_code",orgnazation.getOrgCode());
        }
        page = super.selectPage(page,wrapper);
        return page;
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> initParent(List<Orgnazation> orgnazationList) {
        List<Orgnazation> orgnazationList1 = new ArrayList<>();;
        if (orgnazationList != null && orgnazationList.size() > 0){
            for (Orgnazation orgnazation:orgnazationList){
                orgnazation.setParent(orgnazationMapper.selectById(orgnazation.getParentId()));
                orgnazationList1.add(orgnazation);
            }
        }
        return orgnazationList1;
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> initChildren(List<Orgnazation> orgnazationList) {
        List<Orgnazation> orgnazationList1 = new ArrayList<>();;
        if (orgnazationList != null && orgnazationList.size() > 0){
            for (Orgnazation orgnazation:orgnazationList){
                Map<String,Object> params = new HashMap<>();
                params.put("parent_id",orgnazation.getOrgId());
                orgnazation.setChildren(orgnazationMapper.selectByMap(params));
                orgnazationList1.add(orgnazation);
            }
        }
        return orgnazationList1;
    }

    @Override
    @Cacheable(value = "orgnazation",keyGenerator = "myKeyGenerator")
    public List<Orgnazation> findAllAvailableOrgnazation() {
        EntityWrapper<Orgnazation> wrapper = new EntityWrapper<>();
        wrapper.eq("available",true);
        return orgnazationMapper.selectList(wrapper);
    }

    @Override
    @CacheEvict(value = "orgnazation",allEntries = true)
    public Boolean disableOrg(Orgnazation orgnazation) {
        orgnazation.setAvailable(false);
        return super.updateById(orgnazation);
    }

    @Override
    @CacheEvict(value = "orgnazation",allEntries = true)
    public Boolean ableOrg(Orgnazation orgnazation) {
        orgnazation.setAvailable(true);
        return super.updateById(orgnazation);
    }

    @Override
    @Transactional
    @CacheEvict(value = "orgnazation",allEntries = true)
    public boolean batchDisableOrgs(List<Long> orgIdList) {
        Orgnazation orgnazation = new Orgnazation();
        orgnazation.setAvailable(false);
        EntityWrapper<Orgnazation> wrapper = new EntityWrapper<>();
        wrapper.in("org_id",orgIdList);
        return super.update(orgnazation,wrapper);
    }

    @Override
    @Transactional
    @CacheEvict(value = "orgnazation",allEntries = true)
    public boolean batchAbleOrgs(List<Long> orgIdList) {
        Orgnazation orgnazation = new Orgnazation();
        orgnazation.setAvailable(true);
        EntityWrapper<Orgnazation> wrapper = new EntityWrapper<>();
        wrapper.in("org_id",orgIdList);
        return super.update(orgnazation,wrapper);
    }
}
